package main

import (
	"context"
	"hertzjwt/jwt"
	"hertzjwt/model"

	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/app/server"
)

func main() {
	h := server.Default()
	h.GET("/hello", func(c context.Context, ctx *app.RequestContext) {
		ctx.JSON(200, map[string]any{
			"message": "hello world",
			"code":    200,
			"data":    "ok",
		})
	})

	// init jwt struct
	jwtAuth, err := jwt.NewJwtAuth()
	if err != nil {
		panic(err)
	}
	// login
	h.POST("/login", jwtAuth.LoginHandler)

	// logout
	h.GET("/logout", jwtAuth.MiddlewareFunc(), jwtAuth.LogoutHandler)

	// show user
	h.GET("/user", jwtAuth.MiddlewareFunc(), func(c context.Context, ctx *app.RequestContext) {
		var user = model.User{
			Username: "xiaoxu",
			Email:    "123@163.com",
			Phone:    "1279373683",
			Role:     "admin",
		}
		ctx.JSON(200, user)
	})
	h.Spin()
}
